<%= render 'spree/admin/shared/developers_nav' %>

<%= content_for(:title, 'API Keys') %>

<% content_for :page_actions do %>
  <%= button_link_to 'New API key', new_object_url, class: "btn-primary", icon: 'plus' %>
<% end if can? :create, Spree::OauthApplication %>

<%= content_for :page_alerts do %>
  <div class="alert alert-info mb-3">
    <p>
      Here you can manage API keys to access the <%= link_to 'Platform API', 'https://spreecommerce.org/docs/api-reference/platform/authentication', target: '_blank' %>.
      For headless storefronts you don't need an API key, please use the <%= link_to 'Storefront API', 'https://spreecommerce.org/docs/api-reference/storefront/authentication', target: '_blank' %>.
    </p>
  </div>
<% end %>

<% if @oauth_applications.any? %>
  <div class="table-responsive rounded border mt-4 bg-white shadow-xs">
    <table class="table">
      <thead class="text-muted">
        <tr>
          <th><%= Spree.t(:name) %></th>
          <th>Client ID</th>
          <th><%= Spree.t('admin.oauth_applications.scopes') %></th>
          <th><%= Spree.t('admin.oauth_applications.last_used') %></th>
          <th class="actions"></th>
        </tr>
      </thead>
      <tbody>
        <% @oauth_applications.each do |application| %>
          <tr id="<%= spree_dom_id application %>">
            <td class="w-20"><%= application.name %></td>
            <td class="w-40">
              <div class="input-group" data-controller="password-toggle">
                <%= button_tag class: 'btn hover-gray px-1', data: { action: 'click->password-toggle#password' } do %>
                  <%= icon 'eye', class: 'mr-0' %>
                <% end %>
                <%= password_field_tag :password, application.uid, class: 'form-control-plaintext', data: { password_toggle_target: 'unhide' }, readonly: true %>
              </div>
            </td>
            <td class="w-15">
              <% if application.scopes.include?('write') %>
                <span class="badge badge-warning">
                  <%= icon('pencil', class: 'mr-1') %>
                  Read & Write
                </span>
              <% else %>
                <span class="badge badge-light">
                  <%= icon('eye', class: 'mr-1') %>
                  Read Only
                </span>
              <% end %>
            </td>
            <td class="w-15">
              <% if application.last_used_at %>
                <%= application.last_used_at.to_s %>
              <% else %>
                <span class="text-muted"><%= Spree.t('admin.oauth_applications.never') %></span>
              <% end %>
            </td>
            <td class="actions w-10">
              <%= link_to_edit(application, no_text: true) if can? :edit, application %>
            </td>
          </tr>
        <% end %>
      </tbody>
    </table>
  </div>
<% else %>
  <div class="text-muted p-5 d-flex align-items-center w-100 justify-content-center">
    <%= Spree.t(:no_resource_found, resource: 'API keys') %>,
    <%= link_to_with_icon('plus', Spree.t(:add_one), new_object_url, class: 'btn btn-link') if can? :create, Spree::OauthApplication %>
  </div>
<% end %>
